package com.xingzuo.library_login.ui;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.xingzuo.base.app.AppActivityKey;
import com.xingzuo.base.ui.mvp.base.ui.BaseActivity;
import com.xingzuo.common.event.LoginEventBean;
import com.xingzuo.library_login.R;
import com.xingzuo.library_login.ui.fragment.LoginPhoneFragment;
import com.xingzuo.library_login.ui.fragment.LoginWechatFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

@Route(path = AppActivityKey.LOGIN)
public class LoginActivity extends BaseActivity {
    private LoginPhoneFragment loginPhoneFragment;
    private LoginWechatFragment loginWechatFragment;

    @Override
    public int getView() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        loginPhoneFragment=new LoginPhoneFragment();
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_content,loginPhoneFragment,"loginWechatFragment");
        fragmentTransaction.commit();
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEventBean loginEventBean){
        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        if (loginEventBean.getType() == 0){
            fragmentTransaction.hide(loginWechatFragment).show(loginPhoneFragment).commit();
        }else {
            if (loginWechatFragment == null){
                loginWechatFragment=new LoginWechatFragment();
                fragmentTransaction.add(R.id.fl_content,loginWechatFragment,"loginWechatFragment").commit();
            }else {
                fragmentTransaction.hide(loginPhoneFragment).show(loginWechatFragment).commit();
            }
        }
    }
}
